var nowDate = getDay(0);
var calendar = new lCalendar();
var starttimecalendar = new lCalendar();
var endtimecalendar = new lCalendar();
starttimecalendar.init({
	'trigger': '#startDate',
	'type': 'date',
	'minDate': '2021-01-01'
});
endtimecalendar.init({
	'trigger': '#endDate',
	'type': 'date',
	'minDate': '2021-01-01'
});
function ICalendarClick(){

//	onload()
}
var dataJson;
$(document).ready(function(){
	var date =new Date()
	var year=date.getFullYear();
	var month=date.getMonth()+1;
	if(month>=0&&month<=9){month='0'+month}
	var lastDay=new Date(year,month,0).getDate();
	$('#name').text(sessionStorage.getItem("name"));
	$('#startDate').val(nowDate);
	$('#endDate').val(nowDate)
	$('#searchButton').on('click',function(){
		if($('#startDate').val() > $('#endDate').val()){
			alert("开始日期不能大于结束日期")
		}else{
			getData();
		}
	});
	
	$('#custom').on('click',function(){
			 document.getElementById("renyiriqibox").style.display="";//隐藏
	});
	$('#month').on('click',function(){
			document.getElementById("renyiriqibox").style.display="none";//隐藏
		   	document.getElementById("startDate").value=year+'-'+month+'-'+'01';
			document.getElementById("endDate").value=year+'-'+month+'-'+lastDay;
 			getData(); 
	});
	$('#week').on('click',function(){
			 document.getElementById("renyiriqibox").style.display="none";//隐藏
		     document.getElementById("startDate").value=getCurrentWeekFirstDay("2021-02-23");
			 document.getElementById("endDate").value=getCurrentWeekLastDay("2021-02-23");
		     getData();
	});
	
	$('#planuserSelect').on('change',setTableList);
	getData();
});

$("#timeSelect").change(function(){
var timeval = document.getElementById("timeSelect").value;
var date =new Date()
var year=date.getFullYear();
var month=date.getMonth()+1;
if(month>=0&&month<=9){month='0'+month}
var lastDay=new Date(year,month,0).getDate();
if(timeval=='month'){
	document.getElementById("startDate").value=year+'-'+month+'-'+'01';
	document.getElementById("endDate").value=year+'-'+month+'-'+lastDay;
}
if(timeval=='week'){
	var theweek = date.getDay();
	if(theweek==0)theweek = 7;
	document.getElementById("startDate").value=getNextDate('2021-02-23',1-theweek);
	document.getElementById("endDate").value=getNextDate('2021-02-23',7-theweek);
}

});


function getData(){
	showloading();
	var sendData = {};
	sendData.planstartDate = $('#startDate').val();
	sendData.planendDate = $('#endDate').val();
	sendData.planuserid = sessionStorage.getItem("userId");
	sendData.commituserid = sessionStorage.getItem("userId");
	sendData.checkuserid = sessionStorage.getItem("userId");
	sendData.backuserid = sessionStorage.getItem("userId");
	sendData.lrrid = sessionStorage.getItem("userId");
	console.log(sendData);
	$.ajax({
		url: "../../work_plan_info/getBody"
		,type:'post'
//		,async: false
		,data:JSON.stringify(sendData)
		,contentType: "application/json; charset=utf-8"
		,dataType:'json'
		,success: function(res){
			try {
				//插入计划人
				var planuserJson = res.planuserJson; 
				$('#planuserSelect').empty();
				$('#planuserSelect').append('<option value="">全部</option>');
				for(var el in planuserJson){
				$('#planuserSelect').append('<option value="'+el+'">'+planuserJson[el]+'</option>');
				}
				//循环表格数据
				dataJson = res.data;
				setTableList(dataJson);
				console.log(res);
			} catch (e) {
				console.log(e)
			} finally{
				hideloading();
			}
		}
		,error:function(XMLHttpRequest, textStatus, errorThrown) {
			 console.log(XMLHttpRequest);
			 console.log(textStatus);
			 console.log(errorThrown);
			 hideloading();
		}
	});
}

function setTableList(){
	$('#tableListDiv div[tableDiv]').remove();
	var planuser = $('#planuserSelect').val();
	console.log(planuser)
	for(var el in dataJson){//获取每日的Json
		var dayJson = dataJson[el];
		for(var obj in dayJson){ //获取每人Json
			if(planuser == '' || planuser == obj){//切换下拉框
				var userJson = dayJson[obj];
				var trStr = '';
				for (var i = 0; i < userJson.length; i++) {
					console.log(userJson[i].enddate)
					trStr += '<tr class="thin-border-bottom">'
							+'	<td stype="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;"><span class="num">'+(i+1)+'</span></td>'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;" class="orange" id="'+userJson[i].planuserid+userJson[i].enddate+i+'" >'+userJson[i].content+'</td>'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+userJson[i].enddate+'<br/>'+userJson[i].endtime+'</td>'
							+'</tr>'
				}
				
				$('#tableListDiv').append('<div tableDiv>'
										+'	<div class="widget-box">'
										+'	<div class="widget-header widget-header-flat widget-header-small">'
										+'		<h5>'+userJson[0].planuser+'工作内容</h5>'
										+'       <div class="widget-toolbar" id="shangpinmore">'
										+'			<span>'+userJson[0].plandate+'</span>'
										+'		</div>'
										+'	</div>'
										+'	<div class="widget-body">'
										+'		<div class="widget-main">'
										+'			<div class="clearfix">'
										+'				<div class="widget-main no-padding">'
										+'					<table class="table table-bordered table-striped">'
										+'						<thead class="thin-border-bottom">'
										+'							<tr>'
										+'								<th style="width: 9%;padding-left: 0px;padding-right: 0px;">序号</th>'
										+'								<th>事项</th>'
										+'								<th class="hidden-490" style="width: 30%;">预计完成时间</th>'
										+'							</tr>'
										+'						</thead>'
										+'						<tbody>'
										+							trStr
										+'						</tbody>'
										+'					</table>'
										+'				</div>'
										+'			</div>'
										+'		</div>'
										+'	</div>'
										+'</div>'
										+'</div>');
				userJson = null;
			}
		}
		dayJson = null;
	}
	//点击显示详情
	$("tbody td[id]").on("click",null,function(){
		var html = $(this).html();
		var id = $(this).attr("id");
		var value = $(this).text();
		if(value != undefined){
			//tips层-上
			layer.tips(value, '#'+id, {
				tips: [1, '#0FA6D8'] //还可配置颜色
			});	
		}
		
	});
}
function getNextDate(date,day) { //获取指定日期date的前几天 -day 或者后几天 day 
  var dd = new Date(date);
  dd.setDate(dd.getDate() + day);
  var y = dd.getFullYear();
  var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
  var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
  return y + "-" + m + "-" + d;
};

